home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 285_02 / lex.c < prev    next >
Text File  |  1990-07-08  |  10KB  |  446 lines

  1. /* Token-reader for Bison's input parser,
  2.    Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
  3.  
  4. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the BISON General Public License for full details.
  9.  
  10. Everyone is granted permission to copy, modify and redistribute BISON,
  11. but only under the conditions described in the BISON General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with BISON so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.  
  17.  In other words, you are welcome to use, share and improve this program.
  18.  You are forbidden to forbid anyone else to use, share and improve
  19.  what you give them.   Help stamp out software-hoarding!  */
  20.  
  21. /* 
  22.    lex() is the entry point.  It is called from reader.c.
  23.    It returns one of the token-type codes defined in lex.h.
  24.    When an identifier is seen, the code IDENTIFIER is returned
  25.    and the name is looked up in the symbol table using symtab.c;
  26.    symval is set to a pointer to the entry found.  */
  27.  
  28. #include <stdio.h>
  29. #include <ctype.h>
  30. #include <string.h>
  31. #include "files.h"
  32. #include "symtab.h"
  33. #include "lex.h"
  34. #include "bison.h"
  35.  
  36. extern int lineno;
  37. extern int translations;
  38.  
  39.  
  40. char token_buffer[MAXTOKEN + 1];
  41. bucket *symval;
  42. int numval;
  43.  
  44. static int unlexed;     /* these two describe a token to be reread */
  45. static bucket *unlexed_symval;  /* by the next call to lex */
  46.  
  47.  
  48.  
  49. void init_lex()
  50. {
  51.   unlexed = -1;
  52. }
  53.  
  54.  
  55.  
  56. int
  57. skip_white_space()
  58. {
  59.   register int c;
  60.   register int inside;
  61.  
  62.   c = getc(finput);
  63.  
  64.   for (;;)
  65.     {
  66.       switch (c)
  67.     {
  68.     case '/':
  69.       c = getc(finput);
  70.       if (c != '*')
  71.         fatals("unexpected '/%c' found",c);
  72.  
  73.       c = getc(finput);
  74.  
  75.       inside = 1;
  76.       while (inside)
  77.         {
  78.           if (c == '*')
  79.         {
  80.           while (c == '*')
  81.             c = getc(finput);
  82.  
  83.           if (c == '/')
  84.             {
  85.               inside = 0;
  86.               c = getc(finput);
  87.             }
  88.         }
  89.           else if (c == '\n')
  90.         {
  91.           lineno++;
  92.           c = getc(finput);
  93.         }
  94.           else if (c == EOF)
  95.         fatal("unterminated comment");
  96.           else
  97.         c = getc(finput);
  98.         }
  99.  
  100.       break;
  101.  
  102.     case '\n':
  103.       lineno++;
  104.  
  105.     case ' ':
  106.     case '\t':
  107.     case '\f':
  108.       c = getc(finput);
  109.       break;
  110.  
  111.     default:
  112.       return (c);
  113.     }
  114.     }
  115. }
  116.  
  117.  
  118.  
  119. void unlex(token)
  120. int token;
  121. {
  122.   unlexed = token;
  123.   unlexed_symval = symval;
  124. }
  125.  
  126.  
  127.  
  128. int
  129. lex()
  130. {
  131.   register int c;
  132.   register char *p;
  133.   register int code = 0;
  134.  
  135.   if (unlexed >= 0)
  136.     {
  137.       symval = unlexed_symval;
  138.       c = unlexed;
  139.       unlexed = -1;
  140.       return (c);
  141.     }
  142.  
  143.   c = skip_white_space();
  144.   switch (c)
  145.     {
  146.     case EOF:
  147.       return (ENDFILE);
  148.  
  149.     case 'A':  case 'B':  case 'C':  case 'D':  case 'E':
  150.     case 'F':  case 'G':  case 'H':  case 'I':  case 'J':
  151.     case 'K':  case 'L':  case 'M':  case 'N':  case 'O':
  152.     case 'P':  case 'Q':  case 'R':  case 'S':  case 'T':
  153.     case 'U':  case 'V':  case 'W':  case 'X':  case 'Y':
  154.     case 'Z':
  155.     case 'a':  case 'b':  case 'c':  case 'd':  case 'e':
  156.     case 'f':  case 'g':  case 'h':  case 'i':  case 'j':
  157.     case 'k':  case 'l':  case 'm':  case 'n':  case 'o':
  158.     case 'p':  case 'q':  case 'r':  case 's':  case 't':
  159.     case 'u':  case 'v':  case 'w':  case 'x':  case 'y':
  160.     case 'z':
  161.     case '.':  case '_':
  162.       p = token_buffer;
  163.       while (isalnum(c) || c == '_' || c == '.')
  164.     {
  165.       if (p < token_buffer + MAXTOKEN)
  166.         *p++ = (char) c;
  167.       c = getc(finput);
  168.     }
  169.  
  170.       *p = 0;
  171.       ungetc(c, finput);
  172.       symval = getsym(token_buffer);
  173.       return (IDENTIFIER);
  174.  
  175.     case '0':  case '1':  case '2':  case '3':  case '4':
  176.     case '5':  case '6':  case '7':  case '8':  case '9':
  177.       {
  178.     numval = 0;
  179.  
  180.     while (isdigit(c))
  181.       {
  182.         numval = numval*10 + c - '0';
  183.         c = getc(finput);
  184.       }
  185.     ungetc(c, finput);
  186.     return (NUMBER);
  187.       }
  188.  
  189.     case '\'':
  190.       translations = -1;
  191.  
  192.       /* parse the literal token and compute character code in  code  */
  193.  
  194.       c = getc(finput);
  195.       {
  196.         code = 0;
  197.  
  198.     if (c == '\\')
  199.       {
  200.         c = getc(finput);
  201.  
  202.         if (c <= '7' && c >= '0')
  203.           {
  204.         while (c <= '7' && c >= '0')
  205.           {
  206.             code = (code * 8) + (c - '0');
  207.             c = getc(finput);
  208.           }
  209.         if (code >= 128 || code<0)/* JF this said if(c>=128) */
  210.           fatals("malformatted literal token '\\%03o'",code);
  211.           }
  212.         else
  213.           {
  214.         if (c == 't')
  215.           code = '\t';
  216.         else if (c == 'n')
  217.           code = '\n';
  218.         else if (c == 'r')
  219.           code = '\r';
  220.         else if (c == 'f')
  221.           code = '\f';
  222.         else if (c == 'b')
  223.           code = '\b';
  224.         else if (c == '\\')
  225.           code = '\\';
  226.         else if (c == '\'')
  227.           code = '\'';
  228.         else if (c == '\"') /* JF this is a good idea */
  229.           code = '\"';
  230.         else fatals("invalid literal token '\\%c'",c);
  231.         c = getc(finput);
  232.           }
  233.       }
  234.     else
  235.       {
  236.         code = c;
  237.         c = getc(finput);
  238.       }
  239.     if (c != '\'')
  240.       fatal("multicharacter literal tokens NOT supported");
  241.  
  242.     /* now fill token_buffer with the canonical name for this character
  243.        as a literal token.  Do not use what the user typed,
  244.        so that '\012' and '\n' can be interchangeable.  */
  245.  
  246.     p = token_buffer;
  247.     *p++ = '\'';
  248.     if (code == '\\')
  249.       {
  250.         p = token_buffer + 1;
  251.         *p++ = '\\';
  252.         *p++ = '\\';
  253.       }
  254.     else if (code == '\'')
  255.       {
  256.         p = token_buffer + 1;
  257.         *p++ = '\\';
  258.         *p++ = '\'';
  259.       }
  260.     else if (code >= 040 && code != 0177)
  261.       *p++ = (char) code;
  262.     else if (code == '\t')
  263.       {
  264.         p = token_buffer + 1;
  265.         *p++ = '\\';
  266.         *p++ = 't';
  267.       }
  268.     else if (code == '\n')
  269.       {
  270.         p = token_buffer + 1;
  271.         *p++ = '\\';
  272.         *p++ = 'n';
  273.       }
  274.     else if (code == '\r')
  275.       {
  276.         p = token_buffer + 1;
  277.         *p++ = '\\';
  278.         *p++ = 'r';
  279.       }
  280.     else if (code == '\b')
  281.       {
  282.         p = token_buffer + 1;
  283.         *p++ = '\\';
  284.         *p++ = 'b';
  285.       }
  286.     else if (code == '\f')
  287.       {
  288.         p = token_buffer + 1;
  289.         *p++ = '\\';
  290.         *p++ = 'f';
  291.       }
  292.         else
  293.       {
  294.         *p++ = (char) code / 0100 + '0';
  295.         *p++ = (char) ((code / 010) & 07) + '0';
  296.         *p++ = (char) (code & 07) + '0';
  297.       }
  298.     *p++ = '\'';
  299.     *p = 0;
  300.     symval = getsym(token_buffer);
  301.     symval->class = STOKEN;
  302.     if (! symval->user_token_number)
  303.       symval->user_token_number = code;
  304.     return (IDENTIFIER);
  305.       }
  306.  
  307.     case ',':
  308.       return (COMMA);
  309.  
  310.     case ':':
  311.       return (COLON);
  312.  
  313.     case ';':
  314.       return (SEMICOLON);
  315.  
  316.     case '|':
  317.       return (BAR);
  318.  
  319.     case '{':
  320.       return (LEFT_CURLY);
  321.  
  322.     case '=':
  323.       do
  324.     {
  325.       c = getc(finput);
  326.       if (c == '\n') lineno++;
  327.     }
  328.       while(c==' ' || c=='\n' || c=='\t');
  329.  
  330.       if (c == '{')
  331.         return(LEFT_CURLY);
  332.       else
  333.     {
  334.       ungetc(c, finput);
  335.       return(ILLEGAL);
  336.     }
  337.  
  338.     case '<':
  339.       p = token_buffer;
  340.       c = getc(finput);
  341.       while (c != '>')
  342.     {
  343.       if (c == '\n' || c == EOF)
  344.         fatal("unterminated type name");
  345.  
  346.       if (p >= token_buffer + MAXTOKEN - 1)
  347.         fatals("type name too long (%d max)",MAXTOKEN-1);
  348.  
  349.       *p++ = (char) c;
  350.       c = getc(finput);
  351.     }
  352.       *p = 0;
  353.       return (TYPENAME);
  354.         
  355.  
  356.     case '%':
  357.       return (parse_percent_token());
  358.  
  359.     d